import java.io.BufferedReader;
import java.nio.Buffer;
import java.util.Arrays;
import java.util.*;

public class Solution {
    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length, sum = 0;
        int[] f = new int[n + 1];  //最大子数组和
        int[] g = new int[n + 1];  //最小子数组和
        int max = -0x3f3f3f3f, min = 0x3f3f3f3f;
        for (int i = 1; i <= n; i++) {
            f[i] = Math.max(nums[i - 1], nums[i - 1] + f[i - 1]);
            g[i] = Math.min(nums[i - 1], nums[i - 1] + g[i - 1]);
            max = Math.max(max, f[i]);
            min = Math.min(min, g[i]);
            sum += nums[i - 1];
        }
        return sum == min ? max : Math.max(max, sum - min);
    }

    public static void main(String[] args) {
        //BufferedReader bufferedReader = new BufferedReader(System.in);
    }
}
